package sprites
{
	import UIComponent.LineSkeleton;
	import UIComponent.PointSprite;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.controls.Button;
	import mx.events.PropertyChangeEvent;
	
	import skeletons.ConclusionSkeleton;
	import skeletons.DKRelationSkeleton;
	import skeletons.KConRelationSkeleton;
	import skeletons.KnowledgeSkeleton;

	public class ConclustionSprite extends PointSprite
	{
		//component
		private var highlight:Button;
		private var fromDisconnect:Button;
		
		//status
		private var showFromDisconnect:Boolean=false;
		private var fromDisconnectRelation:KConRelationSkeleton;
		
		public function ConclustionSprite(_conclusion:ConclusionSkeleton, _canvas:MyCanvas)
		{
			super(_conclusion,_canvas);
			
			//component
			highlight=new Button();
			this.addChild(highlight);
			highlight.label="k";
			highlight.x = DEFAULT_MINI_WIDTH - MARGIN_WIDTH*3 - CONVERT_WIDTH - REMOVE_WIDTH - BUTTON_WIDTH;
			highlight.y = MARGIN_HEIGHT;
			highlight.width= BUTTON_WIDTH;
			highlight.height = DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			highlight.addEventListener(MouseEvent.CLICK,onTraceFrom);
			
			fromDisconnect=new Button();
			this.addChild(fromDisconnect);
			fromDisconnect.label="";
			fromDisconnect.x=-1*BUTTON_WIDTH/2;
			fromDisconnect.width= BUTTON_WIDTH/2;
			fromDisconnect.height= DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			fromDisconnect.y= MARGIN_HEIGHT;
			fromDisconnect.addEventListener(MouseEvent.CLICK,onRemoveKConRelation);
			fromDisconnectRelation= null;
			fromDisconnect.visible=false;

			
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this.addEventListener(MouseEvent.DOUBLE_CLICK,onTraceFrom);
		}
	
		override protected function onSelected(evt:MouseEvent):void
		{
			if(canvas.getSelectedCon()==this)
			{
				canvas.setSelectedCon(null);
				this.selected=false;
				this.invalidateDisplayList();
			}
			else
			{
				if(canvas.getSelectedCon()==null)
				{
					//do nothing
				}
				else
				{
					canvas.getSelectedCon().selected=false;
					canvas.getSelectedCon().invalidateDisplayList();
				}
				canvas.setSelectedCon(this);
				this.selected=true;
				this.invalidateDisplayList();
			}
		}
		
		override public function remove():void
		{
			canvas.removeChild(this);
		}
		
		override protected function onRemove(evt:MouseEvent):void
		{
			super.onRemove(evt);
			canvas.curProject.removeConclusion((ConclusionSkeleton)(this.getSkeleton()));
		}
		
		protected function onMouseDown(evt:MouseEvent):void
		{
			canvas.setMouseDownObject(this);
		}
		
		protected function onMouseUp(evt:MouseEvent):void
		{
			var mouseDownObject=canvas.getMouseDownObject();
			if(mouseDownObject==null)
			{
				return;
			}
			
			if(mouseDownObject is KnowledgeSprite)
			{	
				//add KCon relation
				
				var _knowledge:KnowledgeSkeleton = (KnowledgeSkeleton)(((KnowledgeSprite)(mouseDownObject)).getSkeleton());
				var _conclusion:ConclusionSkeleton = (ConclusionSkeleton)(this.getSkeleton());
				
				canvas.curProject.sendKConRelation( _knowledge.getKID(), _conclusion.getConID(), canvas.getSelectedLinkType());
			}
		}
		
		protected function onTraceFrom(evt:MouseEvent):void
		{
			onSetTrace();
//			var tracingObject:Object = canvas.getTrackingObject();
//			if(tracingObject == null)
//			{
//				canvas.setTrackingObject(this);
//				traceFrom(true);
//			}
//			else
//			{
//				if(tracingObject is DataSourceSprite)
//				{
//					var traceData:DataSourceSprite = (DataSourceSprite)(tracingObject);
//					traceData.traceDerive(false);
//					
//					canvas.setTrackingObject(this);
//					traceFrom(true);
//				}
//				else if(tracingObject is KnowledgeSprite)
//				{
//					var traceKnow:KnowledgeSprite = (KnowledgeSprite)(tracingObject);
//					traceKnow.traceDerive(false);
//					traceKnow.traceFrom(false);
//					
//					canvas.setTrackingObject(this);
//					traceFrom(true);
//				}
//				else if(tracingObject is ConclustionSprite)
//				{
//					var traceCon:ConclustionSprite = (ConclustionSprite)(tracingObject);
//					if(traceCon == this)
//					{
//						traceFrom(false);
//						canvas.setTrackingObject(null);
//					}
//					else
//					{
//						traceCon.traceFrom(false);
//						
//						canvas.setTrackingObject(this);
//						traceFrom(true);
//						
//					}		
//				}
//			}
			canvas.invalidateDisplayList();
		}
		
		
		public function traceFrom(_isSelected:Boolean):void
		{
			setHighlight(_isSelected);
			this.invalidateDisplayList();
			var KConList:ArrayList = ((ConclusionSkeleton)(this.getSkeleton()).getKConList());
			if(KConList == null)
			{
				return;
			}
			var i:Number=0;
			for(i=0;i<KConList.length; i++)
			{
				var curKCon:KConRelationSkeleton = (KConRelationSkeleton)(KConList.getItemAt(i));
				curKCon.getSprite().setSelected(_isSelected);
				//curKCon.getFromPointSkeleton().getSprite().setHighlight(_isSelected);
				
				//to be continued
				(KnowledgeSprite)(curKCon.getFromPointSkeleton().getSprite()).traceFrom(_isSelected);
				
				if(canvas.getTrackingObject() is ConclustionSprite)
				{
					(KnowledgeSprite)(curKCon.getFromPointSkeleton().getSprite()).showToConnect(curKCon,_isSelected);
				}
				
			}
		}
		
		public function showFromConnect(_KConRelation:KConRelationSkeleton, _isShow:Boolean):void
		{
			if(_isShow == true)
			{
				fromDisconnect=new Button();
				this.addChild(fromDisconnect);
				fromDisconnect.label="";
				fromDisconnect.x=-1*BUTTON_WIDTH/2;
				fromDisconnect.width= BUTTON_WIDTH/2;
				fromDisconnect.height= DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
				if(expand==true)
				{
					fromDisconnect.y= this.getHeight()/2 - fromDisconnect.height/2;
				}
				else
				{
					fromDisconnect.y= MARGIN_HEIGHT;
				}
				fromDisconnect.addEventListener(MouseEvent.CLICK,onRemoveKConRelation);
				
				fromDisconnectRelation= _KConRelation;
				showFromDisconnect=true;
			}
			else
			{
				this.removeChild(fromDisconnect);
				fromDisconnect=null;
				fromDisconnectRelation=null;
				showFromDisconnect=false;
			}
			this.invalidateDisplayList();
		}
		
		private function onRemoveKConRelation(evt:MouseEvent):void
		{
			canvas.curProject.removeKCon(fromDisconnectRelation,1);
		}
		
		override protected function convertExpand():void
		{
			super.convertExpand();
			if(fromDisconnect!=null)
			{
				if(expand==true)
				{
					fromDisconnect.y= this.getHeight()/2 - fromDisconnect.height/2;
				}
				else
				{
					fromDisconnect.y= MARGIN_HEIGHT;
				}
			}
		}
		
		override public function highlightBefore(_showDelButton:Boolean):void
		{
			var KConList:ArrayList = (ConclusionSkeleton)(getSkeleton()).getKConList();
			if(KConList==null)
				return;
			
			var i:int=0;
			
			for(i=0;i<KConList.length; i++)
			{
				var curKCon:KConRelationSkeleton = (KConRelationSkeleton)(KConList.getItemAt(i));
				curKCon.getSprite().setSelected(true);
				curKCon.getFromPointSkeleton().getSprite().setHighlight(true);
				curKCon.getFromPointSkeleton().getSprite().highlightBefore(false);
				if(_showDelButton==true)
				{
					curKCon.getFromPointSkeleton().getSprite().showDeriveDelButton(curKCon);
				}
			}
		}
		
		override public function showFromDelButton(_relation:LineSkeleton):void
		{
			fromDisconnect.visible=true;
			fromDisconnectRelation = (KConRelationSkeleton)(_relation);
		}
		
		override public function resetHighlight():void
		{
			super.resetHighlight();
			fromDisconnect.visible=false;
		}
		
		override protected function onChanged(evt:PropertyChangeEvent):void
		{
			canvas.curProject.modifyName(this.getSkeleton(),(String)(evt.newValue),3);
		}
		
		override protected function onDiscriptionChanged(evt:PropertyChangeEvent):void
		{
			canvas.curProject.modifyDiscription(this.getSkeleton(),(String)(evt.newValue),3);
		}
	}
	
}